package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.OrderService;
import cn.com.connext.scrm.web.rest.errors.MyErrorCode;
import cn.com.connext.scrm.web.rest.req.DeleteReq;
import cn.com.connext.scrm.web.rest.req.OrderQueryReq;
import cn.com.connext.scrm.web.rest.req.OrderReq;
import com.alibaba.fastjson.JSON;
import com.connext.common.exception.ServiceException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 微信订单
 * @author: ZHaoYul
 * @date: 2021/4/21 11:20
 */

@RestController
@RequestMapping("/api/shopping/wxOrder")
public class WxOrderResource {
    private final Logger log = LoggerFactory.getLogger(WxOrderResource.class);

    @Autowired
    private OrderService orderService;


    /**
     * 创建订单列表
     */
    @PostMapping("/create")
    @ApiOperation(value = "创建订单管理", tags = {"订单列表"}, notes = "创建订单管理")
    public JsonResult save(@RequestBody OrderReq req) {
        log.info("创建订单管理，入参: {}", JSON.toJSONString(req));
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return orderService.save(req);
    }

    /**
     * 查询订单列表
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询订单", tags = {"订单列表"}, notes = "查询订单")
    public JsonResult page(@RequestBody OrderQueryReq req) {
        log.info("查询订单，入参: {}", JSON.toJSONString(req));
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return orderService.page(req);
    }
    /**
     * 详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "查询订单详情", tags = {"查询订单详情"}, notes = "查询订单详情")
    public JsonResult detail(@RequestBody DeleteReq req) {
        log.info("查询订单详情，入参: {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getId())) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return orderService.detail(req.getId());
    }

    /**
     * 创建实物券订单
     */
    @PostMapping("/createPhysicalVoucher")
    @ApiOperation(value = "创建实物券订单", tags = {"创建实物券订单"}, notes = "创建实物券订单")
    public JsonResult savePhysicalVoucher(@RequestBody OrderReq req) {
        log.info("创建实物券订单，入参: {}", JSON.toJSONString(req));
        if (req == null) {
            throw new ServiceException(MyErrorCode.ERROR_INPUT);
        }
        return orderService.savePhysicalVoucher(req);
    }
}
